package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/1/31 20:32
 * @Version 1.0
 **/
public class NextPermutation {


    public void nextPermutation(int[] nums) {
        int len = nums.length;

        for (int i = len - 1; i > 0; i--) {
            if (nums[i] > nums[i - 1]) {
                for (int j = len - 1; j >= 0; j--) {
                    if (nums[j] > nums[i-1]) {
                        int temp = nums[i-1];
                        nums[i-1] = nums[j];
                        nums[j] = temp;
                        for (int p = i, q = len-1; p < q;p++,q--) {
                            temp = nums[p];
                            nums[p] = nums[q];
                            nums[q] = temp;
                        }
                        return;
                    }
                }
            }

        }
        for (int i = 0; i < len/2; i++) {
            int temp = nums[i];
            nums[i] = nums[len-1-i];
            nums[len-1-i] = temp;
        }
    }
}
